import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_stort/html/banner_dao.dart';
import 'package:flutter_stort/html/goods_dao.dart';
import 'package:flutter_stort/model/banner_entity.dart';
import 'package:flutter_stort/model/goods_entity.dart';
import 'package:flutter_stort/utils/jump_util.dart';
import 'package:flutter_stort/widget/banner_view.dart';
import 'package:flutter_stort/widget/load_image.dart';
import 'package:flutter_stort/widget/load_state_view.dart';

import 'goods_details_page.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<BannerEntity> _listBannerData = List<BannerEntity>();
  List<GoodsEntity> _listGoods = List<GoodsEntity>();

  LoadState _loadState = LoadState.LOADING;
  @override
  void initState() {
    _getBanner();
    _getGoods();
    super.initState();
  }

  ///获取轮播图
  void _getBanner() {
    BannerDao.getBanner().then((value) {
      setState(() {
        if (value != null && value.length > 0) {
          _listBannerData.addAll(value);
          if (_listBannerData.isNotEmpty && _listBannerData.length > 0) {
            _listGoods.insert(0, null);
          }
          _loadState = LoadState.SUCCESS;
        } else {
          _loadState = LoadState.EMPTY;
        }
      });
    });
  }

  ///获取商品列表
  void _getGoods() {
    GoodsDao.getGoods().then((value) {
      setState(() {
        if (value != null && value.length > 0) {
          _listGoods.addAll(value);
          _loadState = LoadState.SUCCESS;
        } else {
          _loadState = LoadState.EMPTY;
        }
      });
    });
  }

  ///商品列表
  Widget _getGoodsView(BuildContext context, int position) {
    if (position == 0 && _listGoods[position] == null) {
      return BannerView(
        listBanner: _listBannerData,
        height: 180,
        width: double.infinity,
        padding: 10,
        circular: 10,
      );
    } else {
      return Container(
        margin: EdgeInsets.all(10),
        padding: EdgeInsets.all(6),
        height: 130,
        child: Container(
          child: InkWell(
            onTap: () {
              JumpPageUtils.startNextWidget(
                context,
                GoodsDetailsPage(
                  goods: _listGoods[position],
                ),
              );
            },
            splashColor: Colors.grey,
            child: Row(
              children: [
                LoadImageView(
                  imageUrl: _listGoods[position].url,
                  width: 90,
                  height: 90,
                  fit: BoxFit.cover,
                ),
                SizedBox(width: 10),
                Expanded(
                  flex: 1,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        _listGoods[position].title,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 18,
                        ),
                      ),
                      SizedBox(height: 6),
                      Text(
                        _listGoods[position].content,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: 16,
                        ),
                      ),
                      SizedBox(height: 6),
                      Text(
                        "库存：${_listGoods[position].stock}",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: 16,
                        ),
                      ),
                      SizedBox(height: 4),
                      Text(
                        "￥ ${_listGoods[position].price}",
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 18,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "首页",
          style: TextStyle(
            fontSize: 20,
          ),
        ),
      ),
      body: LoadStateView(
        loadState: _loadState,
        erroRetry: () {},
        successWidget: ListView.builder(
          itemBuilder: _getGoodsView,
          itemCount: _listGoods.length,
        ),
      ),
    );
  }
}
